package edu.mse.camel.client.posterboard.ui.handlers;

import edu.mse.camel.client.core.events.model.ModelUpdateEvent2;
import edu.mse.camel.client.eventbus.pubsub.EventManager;
import edu.mse.camel.client.eventbus.pubsub.ISubscriber;
import edu.mse.camel.client.events.IEvent;
import edu.mse.camel.client.events.peer.CallForAttentionEvent;
import edu.mse.camel.client.events.peer.ReceiveGenericPeerEvent;
import edu.mse.camel.client.events.peer.ViewportChangeEvent;

public class GenericReceiveEventHandler implements ISubscriber {
	
	public GenericReceiveEventHandler() {
		System.out.println("Created");
	}
	
	@Override
	public synchronized void notifyEvent(IEvent event) {
		if (event instanceof ReceiveGenericPeerEvent) {
			ReceiveGenericPeerEvent genericEvent = (ReceiveGenericPeerEvent) event;
			if ( genericEvent.getDataType().equals(ModelUpdateEvent2.class.getCanonicalName())) {
				ModelUpdateEvent2 me = genericEvent.readObject(ModelUpdateEvent2.class);
				// shouldn't cache the regenerated event
				EventManager.getInstance().dispatchEventAsynchronous(me, false);
			}
			if ( genericEvent.getDataType().equals(ViewportChangeEvent.class.getCanonicalName())) {
				ViewportChangeEvent vp = genericEvent.readObject(ViewportChangeEvent.class);
				vp.userID = genericEvent.getUserID(); //IMPORTANT
				// shouldn't cache the regenerated event
				EventManager.getInstance().dispatchEventAsynchronous(vp, false);
			}
			if ( genericEvent.getDataType().equals(CallForAttentionEvent.class.getCanonicalName())) {
				CallForAttentionEvent vp = genericEvent.readObject(CallForAttentionEvent.class);
				// shouldn't cache the regenerated event
				EventManager.getInstance().dispatchEventAsynchronous(vp, false);
			}
			else {
				// it should be okay that we do not understand the actual data type of a generic event
			}
		}
	}

}
